class SongController < ApplicationController

  def search
    @songs = Song.search params[:search]
  end
  
  def search_page
  end
  
  def show
  	@song = Song.find(params[:id])
  end
  
  def create_song
    @song = Song.new(params[:song])
    if @song.save
      flash[:notice] = "Song created successfully."
      redirect_to :controller => 'user', :action => 'home'
    else
      flash[:error] = "Song creation failed."
      redirect_to :action => 'create_song_page'
    end
  end
  
  def create_song_page
  end

  def new_review
    @song = Song.find(params[:id])
  end

  def create_review
    @song_review = User_song_comment.new(params[:song_review])
    if @song_review.save
      flash[:notice] = 'Review saved.'
      redirect_to :action => 'create_review'
    else
      flash[:error] = 'Error saving review.'
      redirect_to :action => 'create_review'
    end
  end
  
  def display
  	if @user.isAdmin == 1
  		redirect_to :action => 'show_admin', :id => params[:id]
  	else
  		redirect_to :action => 'show', :id => params[:id]
  	end
  end
  
  def show_admin
  	if @user.isAdmin == 1
  		@song = Song.find(params[:id])
  	else
      flash[:error] = 'Access Denied.'
      redirect_to :controller => 'user', :action => 'home'
  	end
  end
  
  def destroy
  	@song = Song.find(params[:id])
  	if @user.isAdmin == 1
  	  Song.delete(params[:id])
  	  flash[:notice] = 'Delete Successful.'
  	  redirect_to :controller => 'user', :action => 'home'
  	else
      flash[:error] = 'Delete Failed.'
      redirect_to :controller => 'user', :action => 'home'
  	end
  end

  def create_review
    @song_review = User_song_comment.new(params[:song_review])
    if @song_review.save
      flash[:notice] = 'Review saved.'
      redirect_to :action => 'create_review'
    else
      flash[:error] = 'Error saving review.'
      redirect_to :action => 'create_review'
    end
  end

end
